package com.kgc.skconsumer.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.CwOtherIncome;
import com.kgc.skconsumer.feign.CwOtherIncomeFeign;
import com.kgc.skconsumer.feign.CwWorkFlowFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/CwOtherIncomeController")
public class CwOtherIncomeController {

    private Logger logger= LoggerFactory.getLogger(CwOtherIncomeController.class);

    @Autowired
    private CwOtherIncomeFeign cwOtherIncomeFeign;

    @Autowired
    private CwWorkFlowFeign  cwWorkFlowFeign;

    @RequestMapping("/selectCwOtherIncomeByPage")
    public ResponseMsg selectCwOtherIncomeByPage(@RequestBody Map<String, Object> map){
        logger.info("CwOtherIncomeController selectCwOtherIncomeByPage params {}",map.toString());
        ResponseMsg responseMsg = cwOtherIncomeFeign.selectCwOtherIncomeByPage(map);
        logger.debug("CwOtherIncomeController selectCwOtherIncomeByPage responseMsg {} ",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectCwOtherIncomeByCode")
    public ResponseMsg selectCwOtherIncomeByCode(@RequestBody Map<String, Object> map){
        logger.info("CwOtherIncomeController selectCwOtherIncomeByCode params {}",map.toString());
        ResponseMsg responseMsg = cwOtherIncomeFeign.selectCwOtherIncomeByCode(map);
        logger.debug("CwOtherIncomeController selectCwOtherIncomeByCode responseMsg {}  ",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/updateCwOtherIncome")
    public ResponseMsg updateCwOtherIncome(@RequestBody Map<String, Object> map){
        logger.info("CwOtherIncomeController updateCwOtherIncome params {}",map.toString());
        ResponseMsg responseMsg = cwOtherIncomeFeign.updateCwOtherIncome(map);
        logger.debug("CwOtherIncomeController updateCwOtherIncome responseMsg {} ",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/insertCwOtherIncome")
    public  ResponseMsg insertCwOtherIncome(@RequestBody Map<String, Object> map){
        logger.info("CwOtherIncomeController insertCwOtherIncome params {}",map.toString());
        ResponseMsg responseMsg = cwOtherIncomeFeign.insertCwOtherIncome(map);
        ObjectMapper mapper = new ObjectMapper();

        // 转换 SalesOrder 对象
        CwOtherIncome order = mapper.convertValue(
                map.get("cwOtherIncome"),
                CwOtherIncome.class
        );

        Map<String,Object> variables = new HashMap<>();
        variables.put("code",order.getDocCode());//单据编号
        variables.put("type",order.getTypeId());//流程类型
        //创建一个流程实例
        cwWorkFlowFeign.startProcess(variables);
        logger.debug("CwOtherIncomeController insertCwOtherIncome responseMsg {} ",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/deleteCwOtherIncome")
    public  ResponseMsg deleteCwOtherIncome(@RequestBody Map<String, Object> map){
        logger.info("CwOtherIncomeController selectCwOtherIncome params {}",map.toString());
        ResponseMsg responseMsg=cwOtherIncomeFeign.deleteCwOtherIncome(map);
        logger.debug("CwOtherIncomeController selectCwOtherIncome responseMsg {} ",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/checkCwOtherIncome")
    public  ResponseMsg  checkCwOtherIncome(@RequestBody Map<String, Object> map){
        logger.info("CwOtherIncomeController checkCwOtherIncome params {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",(String) map.get("docCode"));//单据编号
        variables.put("checkManId",(Integer) map.get("checkManId"));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  cwWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        logger.debug("CwOtherIncomeController checkCwOtherIncome responseMsg {} ",responseMsg);
        return responseMsg;
    }

}
